﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using MyLib.Data;
using MyLib.Web.UI;

namespace MyLib.Web.Pages {
	/// <summary>
	/// 
	/// Action: 210
	/// </summary>
	public abstract class SysLogsPage : MyPage {
		/// <summary>
		/// 
		/// </summary>
		protected void DoQuery() {
			var gridView = this.FindDescendantControl<MyGridView>("GridView1");

			gridView.PageIndex = 0;
			gridView.DataBind();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		protected virtual string EvalOperationName() {
			var row = (System.Data.DataRowView)Page.GetDataItem();

			var operationId = row.Get<int>("operation");

			switch (operationId) {
				default:
					return string.Empty;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="causesValidation"></param>
		/// <param name="validationGroup"></param>
		protected virtual void HandleQuery(bool causesValidation, string validationGroup) {
			this.FindDescendantControl<MyPanel>("Panel1").CopyValuesToViewState(ViewState);

			Context.ExecuteSafely(DoQuery);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnInit(EventArgs e) {
			base.OnInit(e);

			// Add object datasources.
			var ds1 = new MyObjectDataSource(typeof(MyLib.Data.SysLogProvider).AssemblyQualifiedName, "FindSysLog");
			ds1.ID = "ObjectDataSource1";
			ds1.SelectCountMethod = "FindSysLogCount";
			ds1.EnableCaching = false;
			ds1.EnablePaging = true;

			ds1.SelectParameters.Add(new MyViewStateParameter {
				Name = "operatorName",
				Type = TypeCode.String,
				Key = "OperatorToMatch"
			});
			ds1.SelectParameters.Add(new MyViewStateParameter {
				Name = "operandName",
				Type = TypeCode.String,
				Key = "OperandToMatch"
			});
			ds1.SelectParameters.Add(new MyViewStateParameter {
				Name = "minDate",
				Type = TypeCode.DateTime,
				Key = "MinDatepoint"
			});
			ds1.SelectParameters.Add(new MyViewStateParameter {
				Name = "maxDate",
				Type = TypeCode.DateTime,
				Key = "MaxDatepoint"
			});

			Controls.Add(ds1);

			// Assign event handlers.

			var gridView1 = this.FindDescendantControl<MyGridView>("GridView1");
			gridView1.DataSourceID = ds1.ID;
			gridView1.AllowPaging = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);

			this.Title = Properties.Main.TitleSysLogs;

			if (!IsPostBack) {
				this.FindDescendantControl<MyPanel>("Panel1").CopyValuesToViewState(ViewState);

				Context.ExecuteSafely(DoQuery);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="source"></param>
		/// <param name="args"></param>
		/// <returns></returns>
		protected override bool OnBubbleEvent(object source, EventArgs args) {
			bool causesValidation = false;
			string validationGroup = string.Empty;

			if (args is CommandEventArgs) {
				IButtonControl commandSource = source as IButtonControl;
				if (commandSource != null) {
					causesValidation = commandSource.CausesValidation;
					validationGroup = commandSource.ValidationGroup;
				}

				var cargs = (CommandEventArgs)args;

				if (string.Equals(cargs.CommandName, "Query", StringComparison.OrdinalIgnoreCase)) {
					HandleQuery(causesValidation, validationGroup);
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		} // end of OnBubbleEvent.

	} // end of CodePage.
}
